package com.linglingC.service.serviceImpl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.linglingC.mapper.EmplyeeMapper;
import com.linglingC.mapper.PropertyItemMapper;
import com.linglingC.pojo.Emplyee;
import com.linglingC.pojo.PageBean;
import com.linglingC.pojo.PropertyItem;
import com.linglingC.pojo.PropertyItemNameId;
import com.linglingC.service.EmplyeeService;
import com.linglingC.service.PropertyItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;


@Service
public class PropertyItemServiceImpl implements PropertyItemService {

    @Autowired
    private PropertyItemMapper propertyItemMapper;


    @Override
    public PageBean findAll(Integer page, Integer pageSize) {
        PageHelper.startPage(page,pageSize);

        List<PropertyItem> list=propertyItemMapper.findAll();

        Page<PropertyItem> p=(Page<PropertyItem>) list;

        PageBean pageBean=new PageBean(p.getTotal(),p.getResult());

        return pageBean;
    }

    @Override
    public PageBean select(String key, String value, Integer page, Integer pageSize) {
        PageHelper.startPage(page,pageSize);

        PropertyItem propertyItem=setKeyValue(key,value);

        List<PropertyItem> list=propertyItemMapper.selectP(propertyItem);

        Page<PropertyItem> p=(Page<PropertyItem>) list;

        PageBean pageBean=new PageBean(p.getTotal(),p.getResult());

        return pageBean;
    }

    @Override
    public void addPropertyItem(PropertyItem propertyItem) {
        propertyItemMapper.addPropertyItem(propertyItem);
    }

    @Override
    public void deleteOnePropertyItem(Integer ID) {
        propertyItemMapper.deleteOnePropertyItem(ID);
    }

    @Override
    public void editPropertyItem(PropertyItem propertyItem) {
        propertyItemMapper.editPropertyItem(propertyItem);
    }

    @Override
    public List<PropertyItemNameId> selectNameID() {

        return propertyItemMapper.findNameId();
    }


    private PropertyItem setKeyValue(String key, String value)
    {
        PropertyItem p=new PropertyItem();
        switch (key){
            case "ID":
                p.setID(Integer.parseInt(value));
                break;
            case "名称":
                p.setName(value);
                break;
            case "价格":
                BigDecimal b=new BigDecimal(value);
                p.setPrice(b);
                break;
            case "周期":
                p.setCycle(value);
                break;
            default:
                break;
        }
        return p;
    }
}
